package com.lulin.mall.order.web;

import com.lulin.common.constant.OrderConstant;
import com.lulin.mall.order.config.AlipayConfig;
import com.lulin.mall.order.service.OrderService;
import com.lulin.mall.order.vo.OrderConfirmVo;
import com.lulin.mall.order.vo.OrderResponseVo;
import com.lulin.mall.order.vo.OrderSubmitVo;
import com.lulin.mall.order.vo.PayVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 订单页面跳转
 *
 * @author lulin
 * @date 2022/12/21 17:43
 */
@Controller
public class OrderWebController {
    @Autowired
    private OrderService orderService;

    @Autowired
    AlipayConfig alipayConfig;

    /**
     * 结算确认页信息
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model) {
        //查询订单确认页的信息
        OrderConfirmVo confirmVo = orderService.confirmOrder();
        model.addAttribute("confirmVo", confirmVo);
        return "confirm";
    }

    /**
     * 结算确认页——提交订单
     */
    @PostMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVo vo, Model model, RedirectAttributes redirectAttributes) {
        OrderResponseVo responseVo = orderService.orderSubmit(vo);
        Integer code = responseVo.getCode();
        if (code == 0) {
            model.addAttribute("orderResponseVo", responseVo);
            //说明下单操作成功
            return "pay";//支付页面
        } else {
            System.out.println("code = " + code);
            String msg = "订单失败";
            if (code == 1) {
                msg = msg + ":重复提交";
            } else if (code == 2) {
                msg = msg + ":锁定库存失败";
            }
            redirectAttributes.addFlashAttribute("msg", msg);
            //说明下单操作失败
            return "redirect:http://order.lulin.com/toTrade";
        }
    }

    /**
     * 订单支付的方法
     *
     * @param orderSn      订单编号
     * @param out_trade_no 支付完后的订单编号
     * @return list订单列表页面
     */
    @GetMapping("/orderPay/returnUrl")
    public String orderPay(@RequestParam(value = "orderSn", required = false) String orderSn,
                           @RequestParam(value = "out_trade_no", required = false) String out_trade_no) {
        //TODO 支付相关的操作
        //System.out.println("订单支付-获取到的orderSn = " + orderSn);
        if (StringUtils.isNotBlank(orderSn)) {
            //支付完成后，后面处理的操作，例如：更新订单状态、更新库存信息、购物车中移除已经支付的商品....等
            orderService.handleOrderComplete(orderSn);
        } else {
            //orderService.updateOrderStatus(out_trade_no,OrderConstant.OrderStatusEnum.TO_SEND_GOODS.getCode());
            orderService.handleOrderComplete(out_trade_no);
        }

        //跳转到订单列表页面
        return "list";
    }

    /**
     * 点击支付宝支付，获取订单相关信息，跳转到支付页面
     * 支付宝沙箱买家账号：yaeokr9912@sandbox.com
     *
     * @param orderSn 订单编号
     * @return
     */
    @GetMapping(value = "/payOrder", produces = "text/html")
    @ResponseBody
    public String payOrder(@RequestParam("orderSn") String orderSn) {

        //根据订单编号，查询出相关的订单信息，分装到PayVo中
        PayVo payVo = orderService.getOrderPay(orderSn);

        //支付宝的支付配置的调用
        String pay = alipayConfig.pay(payVo);
        //System.out.println("pay：" + pay);//获取到前端的提交表单数据
        return "hello , 等待回调支付宝支付..." + pay;
    }
}
